package com.yxt.qmgf.start.ctrl;

import com.yxt.qmgf.common.ClientSession;
import com.yxt.qmgf.home.view.AboutTab;
import com.yxt.qmgf.home.view.HelpingTab;
import com.yxt.qmgf.home.view.HomeListTab;
import com.yxt.qmgf.home.view.SettingTab;
import com.yxt.qmgf.klotski.view.klotskiTab;
import com.yxt.qmgf.minesweeper.view.mineTab;
import com.yxt.qmgf.number.view.numberTab;
import com.yxt.qmgf.snake.view.SnakeTab;
import com.yxt.qmgf.start.vo.LoginVO;
import com.yxt.qmgf.tzfe.view.TzfeTab;
import com.yxt.qmgf.userinfo.view.UserInfoTab;
import com.yxt.qmgf.userinfo.vo.UserInfoVO;
import de.felixroske.jfxsupport.FXMLController;
import javafx.collections.ObservableList;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;


import java.net.URL;
import java.util.ResourceBundle;


@FXMLController
public class MainController implements Initializable {
    private static final TabPane rootTabPane;

    static {
        rootTabPane = new TabPane();
        rootTabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.ALL_TABS);
    }

    @FXML
    private BorderPane bp_root;

    @FXML
    private Button btn_exit;

    @FXML
    private Button btn_backups;

    @FXML
    private Label label_operator; //

    @FXML
    private VBox leftMenuVb;

    public MainController() {

    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        LoginVO vo = new LoginVO();
        UserInfoVO uvo = new UserInfoVO();
        initLeftMenu();


        bp_root.setCenter(rootTabPane);
        vo =(LoginVO) ClientSession.getAttribute("user");


        label_operator.setText("uid：" + vo.getUid());
    }

    public static void addTab(Tab tab) {
        ObservableList<Tab> tabs = rootTabPane.getTabs();
        if (!tabs.contains(tab)) {
            tabs.add(tab);
        }
        rootTabPane.getSelectionModel().select(tab);
    }

    public static void removeTab(Tab tab) {
        ObservableList<Tab> tabs = rootTabPane.getTabs();
        tabs.remove(tab);
    }

    private void initLeftMenu() {
        leftMenuVb.getStylesheets().add("/css/leftmenu.css");
        ObservableList<Node> menuList = leftMenuVb.getChildren();

        TitledPane[] titledPanes = pubLeftMenu();
        if (titledPanes != null) {
            for (TitledPane titledPane : titledPanes) {
                if (titledPane != null) {
                    //  leftMenuVb 是一个可以添加 TitledPane 的容器
                    leftMenuVb.getChildren().add(titledPane);
                }
            }
        }


    }

    private TitledPane[] pubLeftMenu() {
        ListView<Label> lv = new ListView<>();
        ListView<Label> lv1 = new ListView<>();
        Label homepage = new LeftMenuItemLabel("首页");
        Label UserInfo = new LeftMenuItemLabel("个人信息");
        Label setting = new LeftMenuItemLabel("设置");
        Label helping = new LeftMenuItemLabel("帮助与反馈");
        Label about = new LeftMenuItemLabel("关于趣梦工坊");


        lv.getItems().add(homepage);
        lv.getItems().add(UserInfo);
        lv.getItems().add(setting);
        lv.getItems().add(helping);
        lv.getItems().add(about);




        Label game1 = new LeftMenuItemLabel("贪吃蛇");
        Label game2 = new LeftMenuItemLabel("2048");
        Label game3 = new LeftMenuItemLabel("扫雷");
        Label game4 = new LeftMenuItemLabel("数字华容道");
        Label game5 = new LeftMenuItemLabel("数独");
        Label othergame = new LeftMenuItemLabel("更多游戏敬请期待");



        lv1.getItems().add(game1);
        lv1.getItems().add(game2);
        lv1.getItems().add(game3);
        lv1.getItems().add(game4);
        lv1.getItems().add(game5);
        lv.getItems().add(othergame);


        lv.setPrefHeight(5 * (LeftMenuItemLabel.LEFT_MENUITEM_SIZE + 8));//几行前面数字就是几

        lv1.setPrefHeight(6 * (LeftMenuItemLabel.LEFT_MENUITEM_SIZE + 8));//几行前面数字就是几

        homepage.setOnMouseClicked(event -> addTabSafely(HomeListTab.getInstance()));
        UserInfo.setOnMouseClicked(event -> addTabSafely(UserInfoTab.getInstance()));
        setting.setOnMouseClicked(event -> addTabSafely(SettingTab.getInstance()));
        helping.setOnMouseClicked(event -> addTabSafely(HelpingTab.getInstance()));
        about.setOnMouseClicked(event -> addTabSafely(AboutTab.getInstance()));

        game1.setOnMouseClicked(event -> addTabSafely(SnakeTab.getInstance()));
        game2.setOnMouseClicked(event -> addTabSafely(TzfeTab.getInstance()));
        game3.setOnMouseClicked(event -> addTabSafely(mineTab.getInstance()));
        game4.setOnMouseClicked(event -> addTabSafely(klotskiTab.getInstance()));
        game5.setOnMouseClicked(event -> addTabSafely(numberTab.getInstance()));
        othergame.setOnMouseClicked(event -> addTabSafely(null));

        TitledPane titledPane = new TitledPane("信息页", lv);
        TitledPane titledPane1 = new TitledPane("游戏工坊", lv1);

        TitledPane[] tt = new TitledPane[] {titledPane,titledPane1};
        titledPane1.setExpanded(false);
        titledPane.setExpanded(false);

        return tt;
    }

    private static class LeftMenuItemLabel extends Label {

        private static final double LEFT_MENUITEM_SIZE = 30;

        private static final double MAX_VALUE = 9999999;

        private LeftMenuItemLabel(String text) {
            super(text);
            this.setPrefHeight(LEFT_MENUITEM_SIZE);
            this.setMaxWidth(MAX_VALUE);
            this.setAlignment(Pos.CENTER_LEFT);
        }
    }


    private void addTabSafely(Tab tab) {
        if (tab != null) {
            addTab(tab);
        } else {
            // 可以选择记录日志、显示错误消息或执行其他操作
            System.out.println("没有对应tab");
        }
    }



}
